<?php
/**
 * Created by PhpStorm.
 * User: 丶晓
 * Date: 2017/8/25
 * Time: 9:34
 */

namespace app\common;


class Base
{
    /**
     * 发送GET请求的方法
     * @param string $url URL
     * @param bool $ssl 是否为https协议
     * @return string 响应主体Content
     */
    public static function _requestGet($url, $ssl = true, $header = null, $cookie = null, $do_header = false)
    {
        // curl完成
        $curl = curl_init();

        //设置curl选项
        curl_setopt($curl, CURLOPT_URL, $url);//URL
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';
        curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);//user_agent，请求代理信息
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);//referer头，请求来源
        //SSL相关
        if ($ssl) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//检查服务器SSL证书中是否存在一个公用名(common name)。
        }
        if ($header) {
            for ($i = 0; $i < count($header); $i++) {
                $headers[] = $header[$i]; // 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
            }
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        if ($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $do_header);//是否处理响应头
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//curl_exec()是否返回响应结果

        // 发出请求
        $response = curl_exec($curl);
        if (false === $response) {
            return false;
        }
        return $response;
    }

    /**
     * 发送post请求
     * @param string $url URL
     * @param bool $ssl 是否为https协议
     * @return string 响应主体Content
     */
    public static function _requestPost($url, $data, $ssl = true, $header = null, $cookie = false, $do_header = false)
    {
        // curl完成
        $curl = curl_init();

        //设置curl选项
        curl_setopt($curl, CURLOPT_URL, $url);//URL
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';
        curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);//user_agent，请求代理信息
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);//referer头，请求来源
        //SSL相关
        if ($ssl) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//检查服务器SSL证书中是否存在一个公用名(common name)。
            if (isset($ssl['cert']) and isset($ssl['key'])) {
                curl_setopt($curl, CURLOPT_SSLCERT, $ssl['cert']);
                curl_setopt($curl, CURLOPT_SSLKEY, $ssl['key']);
            }
        }
        if ($header) {
            for ($i = 0; $i < count($header); $i++) {
                $headers[] = $header[$i]; // 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
            }
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        if ($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        // 处理post相关选项
        curl_setopt($curl, CURLOPT_POST, true);// 是否为POST请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);// 处理请求数据
        // 处理响应结果
        curl_setopt($curl, CURLOPT_HEADER, $do_header);//是否处理响应头
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//curl_exec()是否返回响应结果

        // 发出请求
        $response = curl_exec($curl);
        curl_close($curl);
        if (false === $response) {
            return false;
        }
        return $response;
    }

    /*
     * 获取文件命名随机值
     */
    public function getOnlyFileKeyName($Path = null)
    {
        return $Path . date('Y-m-d') . '_' . md5(uniqid() . rand(1000, 9999));
    }

    public function toXml($data)
    {
        $xml = '<xml>';
        foreach ($data as $key => $val) {
            $xml .= is_numeric($val) ? '<' . $key . '>' . $val . '</' . $key . '>' :
                '<' . $key . '><![CDATA[' . $val . ']]></' . $key . '>';
        }
        $xml .= '</xml>';

        return $xml;
    }

    /* 已废弃 */
    public function wechatPaySign($key, $data, $way = 'sha256')
    {
        ksort($data);
        $stringA = urldecode(http_build_query($data));
        $stringSignTemp = $stringA . "&key=" . $key;
        return strtoupper(hash_hmac($way, $stringSignTemp, $key));
    }

    /**
     * UTF8字符串加密
     * @param string $string
     * @author Anyon <zoujingli@qq.com>
     * @date 2017/06/26
     * @return string
     */
    public function encode($string)
    {
        $chars = '';
        $len = strlen($string = iconv('utf-8', 'gbk', $string));
        for ($i = 0; $i < $len; $i++) {
            $chars .= str_pad(base_convert(ord($string[$i]), 10, 36), 2, 0, 0);
        }
        return strtoupper($chars);
    }


    /**
     * UTF8字符串解密
     * @param string $string
     * @author Anyon <zoujingli@qq.com>
     * @date 2017/06/26
     * @return string
     */
    public function decode($string)
    {
        $chars = '';
        foreach (str_split($string, 2) as $char) {
            $chars .= chr(intval(base_convert($char, 36, 10)));
        }
        return iconv('gbk', 'utf-8', $chars);
    }

}